(() => {
  /**
   * 获得时间
   */
  function getDuration(seconds) {
    if (isNaN(seconds) || seconds < 0) {
      return "Invalid input";
    }

    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    const remainingSeconds = Math.floor(seconds % 60);

    const formattedHours = String(hours).padStart(2, "0");
    const formattedMinutes = String(minutes).padStart(2, "0");
    const formattedSeconds = String(remainingSeconds).padStart(2, "0");

    if (hours > 0) {
      return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
    } else {
      return `${formattedMinutes}:${formattedSeconds}`;
    }
  }

  function formatDateTime(inputDate) {
    if (typeof inputDate !== "string") {
      return "Invalid input type";
    }
    const curTime = new Date(inputDate);
    const today = new Date();
    const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds

    // 计算与今天的差距，单位：天
    const dayDifference = Math.round(Math.abs((today - curTime) / oneDay));

    // 获取当前月份、日期、小时和分钟
    const currentMonth = curTime.getMonth() + 1;
    const currentDate = curTime.getDate();
    const currentHour = `${curTime.getHours()}`.padStart(2, '0'); // 确保小时和分钟始终为两位数
    const currentMinutes = `${curTime.getMinutes()}`.padStart(2, '0');

    // 星期数组，调整为“星期一”至“星期日”
    const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const weekday = weekdays[curTime.getDay()];

    if (dayDifference === 0) { // 如果是今天
      // 保持原逻辑，但确保格式正确（实际上原来逻辑也是正确的，这里再次明确）
      return `${currentHour}:${currentMinutes}`;
    } else if (dayDifference === 1) { // 如果是昨天
      return `昨天 ${currentHour}:${currentMinutes}`;
    } else if (dayDifference <= 5) { // 如果在过去六天内（不包括今天和昨天）
      return `${weekday} ${currentHour}:${currentMinutes}`;
    } else {
      return `${currentMonth}月${currentDate}日 ${currentHour}:${currentMinutes}`;
    }
  }
  function formatTimeString(timeStr) {
    const [hours, minutes] = timeStr.split(':').map(Number);
    const formattedHours = String(hours).padStart(2, '0');
    const formattedMinutes = String(minutes).padStart(2, '0');
    return `${formattedHours}:${formattedMinutes}`;
  }
  let chartRecords = [];
  chartRecords =
    JSON.parse(localStorage.getItem("chat-content")) || chartRecords;
  const contentEle = document.querySelector(".content");
  contentEle.innerHTML = `

${chartRecords
      .map(
        (v) => {
          if (v.type !== "time") {
            return `
            
        <div class="content-msg">
          <div class="user-left" style="visibility:${v.role === "receiver" ? "visible" : "hidden"
              }">
            <img  src="./headicon/gaojiale.jpg" class="headicon chat-object-headicon" />
          </div>
          <div  class="msg-area  ${v.role === "sender" ? "is-self" : ""}" >
              ${v.role === "sender"
                ? `<div class="msg">${v.type === "message"
                  ? `<img class="check-icon" height="12" width="12" src="./svg/check.svg" alt="" />`
                  : ""
                }${v.type === "phone"
                  ? `<div class="is-call">通话时长${formatTimeString(v.content)}<img style='margin-left:0.5rem' height="22" width="22" src="./svg/phone.svg" alt="" /></div>`
                  : v.content || "&nbsp;"
                }</div> `
                : `<div class="msg">${v.type === "phone"
                  ? `<div class="is-call"><img style='margin-right:0.5rem' height="22" width="22" src="./svg/phone2.svg" alt="" />通话时长${formatTimeString(v.content)}</div>`
                  : v.content || "&nbsp;"
                }</div>`
              }
          </div>
          <div class="user-right" style="visibility:${v.role === "sender" ? "visible" : "hidden"
              }">
            <img src="./headicon/gaojiale.jpg" class="headicon" />  
          </div>
        </div>`
          } else {
            return `<div class="conversation-time">
            <span class="time">${formatDateTime(v.time)}</span>
          </div>`
          }
        }
      )
      .join("")}
`;
})();
